{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "59259bb0-c79a-421f-bf0a-d1514dea4e6f",
   "metadata": {},
   "source": [
    "## How to use this file\n",
    "\n",
    "This file is designed for read the return data pattern of FPGA when the cion is firstly used on one computer. Since the returned data shape could be different for different version of FPGA code, it is necessary to charatcerize it. Once the bytes_number and length of converted data is determined, we can put these two number into \"serial_comm.py\" file to make sure the hardware communication is consistent."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "55b1f20f-672a-4172-932e-c724dcf92635",
   "metadata": {},
   "outputs": [],
   "source": [
    "import serial"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "7808daf4-20af-42b8-b1c1-2c0b67792fa9",
   "metadata": {},
   "outputs": [],
   "source": [
    "FPGA_tester = serial.Serial(\n",
    "    port=\"COM5\",\n",
    "    baudrate=4000000,\n",
    "    parity=serial.PARITY_NONE,\n",
    "    stopbits=serial.STOPBITS_TWO,\n",
    "    timeout = 2,\n",
    "    bytesize=serial.EIGHTBITS)\n",
    "FPGA_tester.reset_input_buffer()\n",
    "FPGA_tester.reset_output_buffer()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4c993415-1273-4df9-9ae0-cc6e6023174d",
   "metadata": {},
   "outputs": [],
   "source": [
    "test_packages = '90000000000000000001 40ffffffff5dfc00000a 40ffffffb2cdfc000002 40fffffec7a5c8000000 40fffffe798500000000 40000000002600000000 40000000002600000000 40000000002600000000 40000000002600000000 40000000002600000000 40000000002600000000 40000000002600000000 40000000002600000000 40fc0000001d00000000 40000000002700000000 60000000002600000000 '"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "87063f03-bacb-4e42-9257-9bc9c3eca20a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "returned data: b'\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x04\\x8b\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00'\n",
      "length of returned data (bytes):  64\n"
     ]
    }
   ],
   "source": [
    "FPGA_tester.write(bytes.fromhex(test_packages))\n",
    "returned_data = FPGA_tester.read_until()\n",
    "print('returned data:', returned_data)\n",
    "print('length of returned data (bytes): ',len(returned_data))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "048fe9e3-c5d3-4953-87e4-7d5af685c115",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "converted data:  [0, 0, 0, 0, 0, 0, 0, 0, 777, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n",
      "length:  32\n"
     ]
    }
   ],
   "source": [
    "bytes_number = 2   #use how many bytes to represent one number\n",
    "converted_data = [int.from_bytes(returned_data[i:i+bytes_number],\"big\") for i in range(0,len(returned_data),bytes_number)] #convert bytes to counts number\n",
    "print(\"converted data: \", converted_data)\n",
    "print(\"length: \", len(converted_data))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
